package com.heba.greenmulu;

import android.content.res.Resources;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.heba.adapter.FmPagerAdapter;
import com.heba.config.CommonConfig;
import com.heba.event.MessageEvent;
import com.heba.fragment.scan.BarScanFragment;
import com.heba.fragment.scan.HistoryScanFragment;
import com.heba.fragment.scan.SavedScanFragment;
import com.heba.fragment.scan.ScanFragment;
import com.heba.greenmulu.scan.ItemHistoryEditActivity;
import com.heba.greenmulu.scan.ItemProductCategoryActivity;
import com.heba.greenmulu.scan.ItemSavedEditActivity;
import com.heba.utils.IndicatorLineUtil;
import com.heba.view.ClearEditTextView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

public class ScanActivity extends BaseActivity {

    private TabLayout tabLayout;
    private ViewPager viewPager;
    private FmPagerAdapter pagerAdapter;
    private ArrayList<Fragment> fragments = new ArrayList<>();
    private String[] strings;
    private int selectIndex = 1;

    private ConstraintLayout bottomLine1;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scan_fragment);
        initTopbar();
        findViewById();
        initView();

    }

    @Override
    protected void findViewById() {


        tabLayout = findViewById(R.id.tablayout);
        viewPager = findViewById(R.id.viewpager);
        bottomLine1 = findViewById(R.id.bottom_line_1);

        txt_title.setText(getResources().getString(R.string.find_product));
    }

    @Override
    protected void initView() {
        EventBus.getDefault().post(new MessageEvent(CommonConfig.SCAN_VISIBLE));


        //添加tab标题数据
        strings = mContext.getResources().getStringArray(R.array.array_scan);

        /**
         * 添加fragment
         */
        fragments.add(new SavedScanFragment());
        fragments.add(new BarScanFragment());
        fragments.add(new HistoryScanFragment());

        /**
         * 添加tab
         */
        for (int i = 0; i < strings.length; i++) {
            tabLayout.addTab(tabLayout.newTab());
        }

        /**
         * 关联viewpager,设置adapter
         */
        tabLayout.setupWithViewPager(viewPager, false);
//        pagerAdapter = new FmPagerAdapter(fragments, getChildFragmentManager());
        pagerAdapter = new FmPagerAdapter(fragments, getSupportFragmentManager());
        viewPager.setAdapter(pagerAdapter);
        viewPager.setOffscreenPageLimit(3);

        /**
         * 设置tab文字,必须在关联viewpager之后设置，否则文字不显示
         */
        for (int i = 0; i < strings.length; i++) {
            tabLayout.getTabAt(i).setText(strings[i]);
        }


        tabLayout.getTabAt(1).select();

        /**
         * 设置指示线长度间隔(单位dp)
         */
        tabLayout.post(new Runnable() {
            @Override
            public void run() {
                IndicatorLineUtil.setIndicator(tabLayout, 0, 0);
            }
        });


        /**
         * tabLayout监听滑动指示卡
         */
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {

                int position = tabLayout.getSelectedTabPosition();
                Resources resources = getResources();
                DisplayMetrics dm = resources.getDisplayMetrics();
                int width3 = dm.widthPixels;


                TranslateAnimation translateAnimation1 = new TranslateAnimation((selectIndex-1)*(width3/3),(position-1)*(width3/3),0,0);
                translateAnimation1.setDuration(300);
                translateAnimation1.setFillEnabled(true);//使其可以填充效果从而不回到原地
                translateAnimation1.setFillAfter(true);//不回到起始位置
                bottomLine1.startAnimation(translateAnimation1);


                translateAnimation1.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        selectIndex = tabLayout.getSelectedTabPosition();
//


                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });
                switch (tab.getPosition()) {
                    case 0:
                        EventBus.getDefault().post(new MessageEvent(CommonConfig.SCAN_MY_PRODUCT));


                        break;
                    case 1:
                        EventBus.getDefault().post(new MessageEvent(CommonConfig.SCAN_BAR_SCAN));


                        break;
                    case 2:
                        EventBus.getDefault().post(new MessageEvent(CommonConfig.SCAN_HISTORY));


                        break;
                    default:
                        break;
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        /**
         * 监听edit事件，显示删除按钮
         */

    }




    @Override
    public void onStart() {
        super.onStart();
        if (!EventBus.getDefault().isRegistered(this))
            EventBus.getDefault().register(this);
    }

    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent event) {
        /**
         * 接收事件
         */
        if (event.getEvent().equals(CommonConfig.SCAN_REFRESH)) {
            if (null != tabLayout && tabLayout.getTabCount() > 0) {
                tabLayout.getTabAt(1).select();
            }
        }
    }
}
